/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aptech.JavaXML.DOMValidator;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author thanhtq
 */
@ManagedBean
@SessionScoped
public class SurveyBeanManage {

    /** Creates a new instance of SurveyBeanManage */
    public SurveyBeanManage() {
    }
    CreateSurvey create = new CreateSurvey();
    ReadSurvey read = new ReadSurvey();
    private int amount = 0;
    CreateQuestionAndAnswer createQA = new CreateQuestionAndAnswer();
    ReadQuestionAndAnswer qAndA = new ReadQuestionAndAnswer();
    boolean checked;
    QuestionBean question;
    List<SurveyBean> surveys;
    List<AnswerBean> answers;
    List<Survey> listSurveys;
    String abc;

    public boolean isChecked() {
        return checked;
    }

    public void setChecked(boolean checked) {
        this.checked = checked;
    }

    public List<Survey> getListSurveys() {
        return read.read();
    }

    public void setListSurveys(List<Survey> listSurveys) {
        this.listSurveys = listSurveys;
    }

    public String getAbc() {
        return abc;
    }

    public void setAbc(String abc) {
        this.abc = abc;
    }

    public List<AnswerBean> getAnswers() {
        return answers;
    }

    public void setAnswers(List<AnswerBean> answers) {
        this.answers = answers;
    }

    public QuestionBean getQuestion() {
        return question;
    }

    public void setQuestion(QuestionBean question) {
        this.question = question;
    }

    public List<SurveyBean> getSurveys() {
        displaySurvey();
        return surveys;
    }

    public void setSurveys(List<SurveyBean> surveys) {
        this.surveys = surveys;
    }

    public void displaySurvey() {
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        String surveyId = "";
        if (request.getParameter("surveyId") != null) {
            surveyId = request.getParameter("surveyId");
            SurveyBean surveyBean;
            List<SurveyBean> lsSurvey = new ArrayList<SurveyBean>();
            Survey survey = read.readSurvey("D:\\Project\\thanhtv00011\\Survey\\Survey.xml", surveyId);
            surveyBean = new SurveyBean();
            surveyBean.setIdSurvey(survey.getIdSurvey());
            surveyBean.setContentSurvey(survey.getContentSurvey());
            surveyBean.setCommenSurvey(survey.getCommenSurvey());
            surveyBean.setQuestionId(survey.getQuestionId());
            List<QuestionBean> lsQA = new ArrayList<QuestionBean>();
            System.out.println(surveyId+"dfdfdf" + survey.getContentSurvey());
            for (String id : survey.getQuestionId()) {
                displayQA(id);
                lsQA.add(question);
            }
            surveyBean.setQuestion(lsQA);
            lsSurvey.add(surveyBean);
            setSurveys(lsSurvey);
        }
    }

    public void displayQA(String id) {
        Question ques = qAndA.readQuestion("D:\\Project\\thanhtv00011\\Survey\\Survey.xml", id).get(0);
        question = new QuestionBean();
        question.setContent(ques.getContent());
        question.setId(ques.getId());
        question.setNumber("0");
        displayAnswer(id);
        question.setAmount(amount);
        question.setAnswers(answers);
    }

    public void displayAnswer(String id) {
        AnswerBean answerBean;
        amount = 0;
        List<AnswerBean> lsAnswer = new ArrayList<AnswerBean>();
        int number = 1;
        for (Answer answer : qAndA.readAnswer("D:\\Project\\thanhtv00011\\Survey\\Survey.xml", id)) {

            double rate = 0;
            if (Integer.parseInt(answer.getRate()) > 0) {
                rate = Double.parseDouble(answer.getRate()) * 100 / qAndA.numberManTl;
            }
            DecimalFormat df = new DecimalFormat("#.##");
            answerBean = new AnswerBean();
            answerBean.setId(answer.getId());
            answerBean.setOption(answer.getOption());
            answerBean.setQuesId(answer.getQuesId());
            answerBean.setRate(answer.getRate());
            answerBean.setNumber(String.valueOf(number));
            answerBean.setRating(String.valueOf(df.format(rate)) + "%");
            lsAnswer.add(answerBean);
            amount += Integer.parseInt(answer.getRate());
            number++;
        }
        setAnswers(lsAnswer);

    }

    public String display() {
        for (SurveyBean surveyBean : surveys) {
            for (QuestionBean quest : surveyBean.getQuestion()) {
                for (AnswerBean ans : quest.getAnswers()) {
                    if (ans.isChecked()) {
                        int rate = Integer.parseInt(ans.getRate()) + 1;
                        createQA.update(rate, ans.getId());
                        break;
                    }
                    System.out.println(ans.getRate() + " : " + ans.isChecked() + " rate : " + ans.getRating() + " : " + quest.getAmount());
                }
            }
        }
        return "finish";
    }
}
